package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckgroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/checkgroup")
public class CheckgroupController {

    @Reference
    CheckgroupService checkgroupService;

    @RequestMapping("/add")
//    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkItemIds){
//        try {
//            checkgroupService.add(checkGroup,checkItemIds);
//            return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, MessageConst.ADD_CHECKGROUP_FAIL);
//        }
//    }

    public Result add(@RequestBody Map<String, Object> map) {
        JSONObject jsonObject = (JSONObject) map.get("checkgroup");
        CheckGroup checkGroup = jsonObject.toJavaObject(CheckGroup.class);
        JSONArray jsonArray = (JSONArray) map.get("checkitemIds");
        Integer[] checkItemIds = jsonArray.toArray(new Integer[]{});

        try {
            checkgroupService.add(checkGroup, checkItemIds);
            return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkgroupService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            CheckGroup checkGroup = checkgroupService.findById(id);
            return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findCheckitemIdsByCheckGroupId")
    public Result findCheckitemIdsByCheckGroupId(Integer id) {
        try {
            Integer[] checkitemIds = checkgroupService.findCheckitemIdsByCheckGroupId(id);
            return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){
        try {
            checkgroupService.edit(checkitemIds,checkGroup);
            return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try {
            checkgroupService.deleteById(id);
            return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckGroup> checkGroupList = checkgroupService.findAll();
            return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
        }
    }
}
